package com.ruoyi.common.utils.qywx;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.project.system.domain.SysDate;
import com.ruoyi.project.system.service.ISysDateService;
import com.ruoyi.project.system.service.impl.SysDateServiceImp;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class QywxUtil {

    @Value("${qywx.corpid}")
    private static String corpid = "ww4f283fc13177191f";

    @Value("${qywx.corpsecret}")
    private static String corpsecret = "hhloEe0FCW7_6sp7H97eIX4imRbeLMbct_THRCPq2jk";

    private static String access_token = "o6feAAc_vzP9n7jS6-V3xofNTbT9XilKgkGrpnvSLvB-zEuvWFOkbYgxhQr3jiIq2BBi6wlnoLrnruxxyypH7ogCAbmyvWOu2wGbwLu74FBU7D4p6m8gdfqgmla63sESvJlafRasqvOxx2p2rJ0vQgBk2wrUTrHWqiHcQXTrqu1baUn97-99xjeJtnkKL-qAchr9a61OQkEQppRrwEscAw";

    public static String getAccessToken() {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";

        String result = HttpUtils.sendGet(url, "corpid=" + corpid + "&corpsecret=" + corpsecret);

        JSONObject jsonObject = JSONObject.parseObject(result);

        if("0".equals(jsonObject.getString("errcode"))){
            return jsonObject.getString("access_token");
        }

        return "";
    }

    public static String sendMsg()
    {
        Map map = new HashMap();
        map.put("touser", "pengfei");
        map.put("msgtype", "text");
        map.put("agentid", 1004);

        Map content = new HashMap();
        content.put("content", "您好，以下店铺即将到期：\n 店铺111");
        map.put("text", content);
        map.put("safe", 0);
        map.put("enable_id_trans", 0);
        map.put("enable_duplicate_check", 0);
        map.put("duplicate_check_interval", 1800);

        String url = " https://qyapi.weixin.qq.com/cgi-bin/message/send";

        String result = HttpUtils.sendPost(url, "access_token=" + access_token);

        return "";
    }

    public static void main(String[] args) {

        //String result = sendMsg();

        ISysDateService dateService = new SysDateServiceImp();
        Date startDate = DateUtils.dateTime("yyyy-MM-dd", "2025-01-01");
        do{
            SysDate sysDate = new SysDate();
            sysDate.setDate(startDate);
            System.out.println("================== 日期：" + DateUtils.parseDateToStr("yyyy-MM-dd", startDate));
            dateService.insertDate(sysDate);
            startDate = DateUtils.addDays(startDate, 1);
        }while ("2025".equals(DateUtils.parseDateToStr("yyyy", startDate)));
    }
}
